<?php

namespace app\command;

use app\common\model\GoodsList;
use app\common\model\Order;
use app\common\model\OrderList;
use app\common\model\RankMonth as RankMonthModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;


class RankMonth extends Command
{
    protected function configure()
    {
            $this->setName('RankMonth')->setDescription('月榜定榜');
    }

    protected function execute(Input $input, Output $output)
    {
        #上个月开始时间
        $beginLastMonth = strtotime(date('Y-m-01', strtotime('-1 month')));
        #上个月结束时间
        $endLastMonth = strtotime(date('Y-m-t', strtotime('-1 month'))) + 86399;

//        echo date('Y-m-d H:i:s',$beginLastMonth);
//        echo "\r\n";
//        echo date('Y-m-d H:i:s',$endLastMonth);
//        die;
        #添加时间
        $addtime = time();
        #月时间范围
        $month_time = date('Y-m-d H:i:s', $beginLastMonth) . ' - ' . date('Y-m-d H:i:s', $endLastMonth);
        $data = Order::field('user_id,sum(`order_total`) as order_total')
            ->append(['user_info'])
            ->where('addtime', 'between', [$beginLastMonth, $endLastMonth])
            ->where('status', '=', 1)
            ->where('order_type', '<', 5)
            ->group('user_id')
            ->order('order_total desc,user_id asc')
            ->limit(30)
            ->select()->toArray();

        Db::startTrans();
        $res = [];
        #定榜记录
        $save_rank = [];
        foreach ($data as $key => $value) {
            $goods_id = -2;
            $rank = $key + 1;
            $order_total = $value['order_total'] * 1;
            $order_list_id = 0;
            #奖品信息
            $prize_info = GoodsList::where('goods_id', '=', $goods_id)
                ->where('rank', '=', $rank)
                ->find();
            if ($prize_info) {
                $res[] = $order_list_id = OrderList::insertGetId([
                    'order_id' => 0,
                    'user_id' => $value['user_id'],
                    'status' => 0,#0未操作 1选择兑换 2选择发货
                    'goods_id' => $goods_id,
                    'num' => 0,
                    'shang_id' => $prize_info['shang_id'],
                    'goodslist_id' => $prize_info['id'],
                    'goodslist_title' => $prize_info['title'],
                    'goodslist_imgurl' => $prize_info['imgurl'],
                    'goodslist_price' => $prize_info['price'],
                    'goodslist_money' => $prize_info['money'],
                    'goodslist_type' => $prize_info['goods_type'],
                    'goodslist_sale_time' => $prize_info['sale_time'],
                    'addtime' => $addtime,
                    'prize_code' => $prize_info['prize_code'],
                    'order_type' => 8,
                ]);
                $prize_title = $prize_info['title'];
                $prize_imgurl = $prize_info['imgurl'];
            } else {
                $prize_title = '';
                $prize_imgurl = '';
            }
            $save_rank[] = [
                'user_id' => $value['user_id'],
                'rank' => $rank,
                'money' => $order_total,
                'month_time' => $month_time,
                'addtime' => $addtime,
                'order_list_id' => $order_list_id,
                'prize_title' => $prize_title,
                'prize_imgurl' => $prize_imgurl,
            ];
        }
        #定榜记录
        if ($save_rank) {
            $res[] = RankMonthModel::insertAll($save_rank);
        }
        if (resCheck($res)) {
            Db::commit();
            $time = date('Y-m-d H:i:s', time());
            $output->writeln('success' . $time);
        } else {
            Db::rollback();
            $time = date('Y-m-d H:i:s', time());
            $output->writeln('error' . $time);
        }
    }


}
